<?php

//getlanglat("Stockholm", "Göteborg");
//echo getlanglat("Stockholm","Göteborg");

//echo findtrain(3038);
//findtrain(3038);

if(count($_POST)>0){
    $post = array();
    foreach($_POST as $p){
	$post[] = $p;
    }

    if($_POST['info']=='trains'){
	echo findtrain($post[1]);
    }

    if($_POST['info']=='location'){
	echo getlanglat($post[1],$post[2]);
    }
}

function findtrain($id){
    $now = date('Ymd');
    $pagecontent = file_get_contents("http://www5.trafikverket.se/trafikinformation/(S(yep43gqrsqgrdn55xwtfo045))/WebPage/TrafficSituationTrain.aspx?JF=7&train=$now,$id");
    //echo $pagecontent = file_get_contents("http://www5.trafikverket.se/trafikinformation/(S(yep43gqrsqgrdn55xwtfo045))/WebPage/TrafficSituationTrain.aspx?JF=7&train=20110228,$id");

    //echo $pagecontent = utf8_encode($pagecontent);

    $arraycontent = explode("<", $pagecontent);

    //print_r($arraycontent);

    $returnstring = array();
    $returnstring[0] = "";

    $modvar = 0;

    for($i=0; $i<count($arraycontent);$i++){
	$temp = array();
	if($arraycontent[$i] == 'div class="textLinks">'){
	    //fetch stationname
	    $stationstring = $arraycontent[$i+1];
	    $cutmarker = strpos($stationstring, ">") + 1;
	    $stationname = substr($stationstring, $cutmarker);
	    $temp['station'] = $stationname;
	    //fetch arrival-time
	    $ordarrivalstring = $arraycontent[$i+6];
	    $cutmarker = strpos($ordarrivalstring, ">") + 1;
	    $ordarrival = substr($ordarrivalstring, $cutmarker);
	    $temp['ordinaryarrival'] = $ordarrival;
	    //fetch new arrivaltime
	    for($j=$i;$j<($i+35);$j++){
		if( ($arraycontent[$j] == 'i>Ankom') ){
//		if( ($arraycontent[$j] == 'i>Ankom') || ($arraycontent[$j] == substr($arraycontent[$j], 3)) ){
		    $newarrivalstring = $arraycontent[$j+4];
		    $cutmarker = strpos($newarrivalstring, ">") + 1;
		    $newarrival = substr($newarrivalstring, $cutmarker);
		    $temp['newarrival'] = $newarrival;
		    $j = 1000;
		}
	    }
	    //echo "<br><br><br>";
	    //fetch departuretime
	    for($j=$i;$j<($i+35);$j++){
		if($arraycontent[$j] == 'td valign="top" Headers="Departure" width="75">'){
		    $orddeparturestring = $arraycontent[$j+1];
		    $cutmarker = strpos($orddeparturestring, ">") + 1;
		    $orddeparture = substr($orddeparturestring, $cutmarker);
		    $temp['ordinarydeparture'] = $orddeparture;
		    $j = 1000;
		}
	    }
	    for($j=$i;$j<($i+35);$j++){
		if($arraycontent[$j] == 'i>Avgick'){
		    $newdeparturestring = $arraycontent[$j+4];
		    $cutmarker = strpos($newdeparturestring, ">") + 1;
		    $newdeparture = substr($newdeparturestring, $cutmarker);
		    $temp['newdeparture'] = $newdeparture;
		    $j = 1000;
		}
	    }
	    if(!isset($temp['ordinarydeparture'])){
		$findsecond = 1;
		for($j=$i;$j<($i+35);$j++){
		    if($arraycontent[$j] == 'td valign="top" width="75">'){
			if($findsecond==2 && isset($temp['newdeparture'])){
			    $ordarrivalstring = $arraycontent[$i+6];
			    $cutmarker = strpos($ordarrivalstring, ">") + 1;
			    $ordarrival = substr($ordarrivalstring, $cutmarker);
			    $temp['ordinarydeparture'] = $ordarrival;
			}
			$findsecond++;
		    }
		}
	    }

	    if( isset($temp['ordinarydeparture']) ){
		if($temp['ordinarydeparture'] == false){
		    $ordarrivalstring = $arraycontent[$i+8];
		    $cutmarker = strpos($ordarrivalstring, ">") + 1;
		    $ordarrival = substr($ordarrivalstring, $cutmarker);
		    $temp['ordinarydeparture'] = $ordarrival;
		}
	    }

//	    if(isset($temp['ordinarydeparture'])){
//		echo $temp['station'];
//	    }

	    if(!isset($temp['newdeparture']) && isset($temp['ordinarydeparture'])){
		$temp['newdeparture'] = $temp['ordinarydeparture'];
	    }

	    $returnstring[] = $temp;
	}

    }

    //echo count($returnstring);

    if(count($returnstring)<2){
	$tmp = array();
	$tmp['result'] = 0;
	$returnstring[0] = $tmp;
    }else{
	$tmp = array();
	$tmp['result'] = 1;
	$returnstring[0] = $tmp;
    }

    $json = json_encode($returnstring);

    return $json;
}

function getlanglat($address1,$address2){
    $returnstring = array();
    $tmp = array();

    $xmlurl = file_get_contents("http://maps.googleapis.com/maps/api/geocode/xml?address=$address1&sensor=true");

    $xml = new SimpleXMLElement($xmlurl);

    $latarr = $xml->xpath('/GeocodeResponse/result/geometry/location/lat');
    $lat = $latarr[0];
    $tmp['lat'] = (string)$lat;

    $langarr = $xml->xpath('/GeocodeResponse/result/geometry/location/lng');
    $lang = $langarr[0];
    $tmp['lang'] = (string)$lang;

    $returnstring[] = $tmp;

    ////////////////////////////

    $xmlurl = file_get_contents("http://maps.googleapis.com/maps/api/geocode/xml?address=$address2&sensor=true");

    $xml = new SimpleXMLElement($xmlurl);

    $latarr = $xml->xpath('/GeocodeResponse/result/geometry/location/lat');
    $lat = $latarr[0];
    $tmp['lat'] = (string)$lat;

    $langarr = $xml->xpath('/GeocodeResponse/result/geometry/location/lng');
    $lang = $langarr[0];
    $tmp['lang'] = (string)$lang;

    $returnstring[] = $tmp;
    
    //print_r($returnstring);

    $json = json_encode($returnstring);

    return $json;
}
?>